(***********************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which typically
will have the same name as this file except ending in ".nb" instead of
".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent to
using the Evaluate Initialization Cells menu command in the front end.

DO NOT EDIT THIS FILE.  This entire file is regenerated automatically 
each time the parent Notebook file is saved in the Mathematica front end.
Any changes you make to this file will be overwritten.
***********************************************************************)





























































Off[General::spell];
Off[General::spell1];
BeginPackage["`IntegerSmithNormalForm`"]









IntegerSmithNormalForm::usage="IntegerSmithNormalForm[A] gives, for a matrix over the integers A, a list {D,{P,Q}}, D being the integer Smith Normal Form related to A, and P,Q being the change basis matrices, so that D=P\[CenterDot]A\[CenterDot]Q. Notice that the matrices P and Q are not uniquely determined."









Begin["`Private`"]
{$Context,$ContextPath}











M[i_,j_,t_,k_]:=ReplacePart[IdentityMatrix[k],t,{i,j}];
T[i_,j_,k_]:=
    Module[{m,n},m=IdentityMatrix[k];n=m[[i]];m[[i]]=m[[j]];m[[j]]=n;m];
Completar[C_,m_,n_]:=
    Module[{k,i,j,ii},i=m-Length[C];k={};
      Do[k=Append[k,Table[0,{ii,1,n}]],{j,1,i}];
      Do[k=Append[k,Join[Table[0,{ii,1,n-Length[C[[1]]]}],C[[j]]]],{j,1,
          Length[C]}];k];
IntegerSmithNormalForm[A_]:=
    Module[{k,C,coli,colj,P,Q,m,n,H,i,it,control},m=Length[A];
      n=Length[A[[1]]];P={IdentityMatrix[m]};Q={IdentityMatrix[n]};C=A;i=1;
      Fin=Max[Abs[Take[C,{i,m},{i,n}]]]>0;
      While[
        Fin,{coli,colj}=
          Position[Completar[Take[Abs[C],{i,m},{i,n}],m,n],
              Min[Select[Flatten[Take[Abs[C],{i,m},{i,n}]],#1>0&]]][[1]];
        P=Prepend[P,T[i,coli,m]];Q=Append[Q,T[i,colj,n]];it=i+1;
        C=First[P].C.Last[Q];control=True;
        While[it\[LessEqual]n,
          If[IntegerQ[C[[i,it]]/C[[i,i]]],it=it+1,
            Q=Append[Q,M[i,it,-Quotient[C[[i,it]],C[[i,i]]],n].T[it,i,n]];
            C=C.Last[Q];control=False;it=n+1]
          ];
        If[control,
          Do[Q=Append[Q,M[i,it,-C[[i,it]]/C[[i,i]],n]];
            C=C.Last[Q],{it,i+1,n}];it=i+1;
          
          While[it\[LessEqual]m,
            If[IntegerQ[C[[it,i]]/C[[i,i]]],it=it+1,
              P=Prepend[P,T[it,i,m].M[it,i,-Quotient[C[[it,i]],C[[i,i]]],m]];
              C=First[P].C;control=False;it=m+1]
            ];
          If[control,
            Do[P=Prepend[P,M[it,i,-C[[it,i]]/C[[i,i]],m]];
              C=First[P].C,{it,i+1,m}];
            Catch[Do[
                
                Do[If[IntegerQ[C[[coli,colj]]/C[[i,i]]],,
                    P=Prepend[P,M[i,coli,1,m]];C=First[P].C;control=False;
                    Throw[False]],{colj,i+1,n}],{coli,i+1,m}];Throw[True]];
            If[ control,P=Prepend[P,M[i,i,Sign[C[[i,i]]],m]];C=First[P].C;
              i=i+1;Fin=Max[Abs[Take[C,{i,m},{i,n}]]]>0]
            ];
          ];
        ];
      P=Fold[Dot,IdentityMatrix[m],P];
      Q=Fold[Dot,IdentityMatrix[n],Q];{C,{P,Q}}
      ];



End[ ]









EndPackage[ ]





















